Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | 'use client' import { useState, useMemo, useCallback, useRef } from 'react' import { useParams, useSearchParams, useRouter } from 'next/navigation' import { AppNavBar } from '@/components/AppNavBar' import { EuclidCanvas } from '@/components/toys/euclid/EuclidCanvas' import { PlayerPicker } from '@/components/shared/PlayerPicker' import { useEuclidProgress, useMarkEuclidComplete } from '@/hooks/useEuclidProgress' import { useUserPlayers } from '@/hooks/useUserPlayers' import { usePlayerSessionPreferences } from '@/hooks/usePlayerSessionPreferences' import { getProposition, getUnlockedBy, getNextProp, } from '@/components/toys/euclid/data/propositionGraph' import { PROP_REGISTRY, ALTERNATE_PROOFS } from '@/components/toys/euclid/propositions/registry' import { resolveKidLanguageStyle } from '@/lib/kidLanguageStyle' import { getAgeFromBirthday } from '@/lib/playerAge' export default function EuclidPropPage() { const params = useParams() const searchParams = useSearchParams() const router = useRouter() const propId = Number(params.id) || 1 const prop = getProposition(propId) const title = prop ? `Euclid I.${propId}` : `Euclid I.${propId}` // ── Proof variant state ── const [proofVariant, setProofVariant] = useState<string | undefined>(undefined) const canonical = PROP_REGISTRY[propId] const alternates = ALTERNATE_PROOFS[propId] ?? [] const allVariants = canonical ? [canonical, ...alternates] : [] const hasAlternates = allVariants.length > 1 const selectedProp = useMemo(() => { if (!proofVariant) return canonical return allVariants.find((v) => v.proofVariant === proofVariant) ?? canonical // eslint-disable-next-line react-hooks/exhaustive-deps }, [propId, proofVariant]) // Player state — initialized from URL search param if present const [selectedPlayerId, setSelectedPlayerId] = useState<string | null>( searchParams.get('player') ) const { data: players } = useUserPlayers() const selectedPlayer = useMemo( () => (selectedPlayerId ? players?.find((p) => p.id === selectedPlayerId) : null), [players, selectedPlayerId] ) const { data: preferences } = usePlayerSessionPreferences(selectedPlayerId) const languageStyle = useMemo( () => resolveKidLanguageStyle( preferences?.kidLanguageStyle, getAgeFromBirthday(selectedPlayer?.birthday) ), [preferences?.kidLanguageStyle, selectedPlayer?.birthday] ) const { data: completedList } = useEuclidProgress(selectedPlayerId) const markComplete = useMarkEuclidComplete(selectedPlayerId) // Store mutation in a ref so handleComplete doesn't depend on it const markCompleteRef = useRef(markComplete) markCompleteRef.current = markComplete const completed = useMemo(() => new Set(completedList ?? []), [completedList]) // Stable callback — uses refs to avoid depending on mutation object const handleComplete = useCallback( (completedPropId: number) => { markCompleteRef.current.mutate(completedPropId) }, [] // stable — reads current values from refs ) const afterCompletion = useMemo(() => { const after = new Set(completed) after.add(propId) return after }, [completed, propId]) // Compute unlocked props and next prop for the overlay const unlocked = useMemo(() => getUnlockedBy(propId, afterCompletion), [propId, afterCompletion]) const nextPropId = useMemo( () => unlocked[0] ?? getNextProp(afterCompletion), [unlocked, afterCompletion] ) const navigateWithPlayer = useCallback( (path: string) => { const playerParam = selectedPlayerId ? `?player=${encodeURIComponent(selectedPlayerId)}` : '' router.push(`${path}${playerParam}`) }, [selectedPlayerId, router] ) return ( <div data-component="euclid-page" style={{ width: '100vw', height: '100dvh', overflow: 'hidden', background: '#FAFAF0', display: 'flex', flexDirection: 'column', }} > <AppNavBar navSlot={ <div style={{ display: 'flex', alignItems: 'center', gap: '8px' }}> <button type="button" onClick={() => navigateWithPlayer('/toys/euclid')} style={{ display: 'inline-flex', alignItems: 'center', gap: 8, padding: '6px 10px', borderRadius: 10, border: '1px solid rgba(203, 213, 225, 0.8)', background: 'rgba(255, 255, 255, 0.9)', color: 'rgba(55, 65, 81, 1)', fontSize: '14px', fontWeight: 600, cursor: 'pointer', }} aria-label="Open Euclid map" > {title} <span aria-hidden="true" style={{ display: 'inline-flex', alignItems: 'center', opacity: 0.5, }} > <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" > <rect x="3" y="3" width="7" height="7" /> <rect x="14" y="3" width="7" height="7" /> <rect x="3" y="14" width="7" height="7" /> <rect x="14" y="14" width="7" height="7" /> </svg> </span> </button> <PlayerPicker selectedPlayerId={selectedPlayerId} onSelect={setSelectedPlayerId} /> </div> } /> {/* Proof variant selector — shown when multiple proofs exist */} {hasAlternates && ( <div data-element="proof-selector" style={{ position: 'absolute', top: 'calc(var(--app-nav-height) + 8px)', left: '50%', transform: 'translateX(-50%)', zIndex: 15, display: 'flex', gap: 2, padding: 2, borderRadius: 16, background: 'rgba(255, 255, 255, 0.9)', border: '1px solid rgba(203, 213, 225, 0.6)', boxShadow: '0 2px 8px rgba(0,0,0,0.08)', backdropFilter: 'blur(8px)', }} > {allVariants.map((variant) => { const variantId = variant.proofVariant const isActive = proofVariant === variantId || (!proofVariant && !variantId) const label = variant.proofLabel ?? (variantId ? variantId : 'Euclid') return ( <button key={variantId ?? 'canonical'} type="button" onClick={() => setProofVariant(variantId)} style={{ padding: '4px 14px', borderRadius: 14, border: 'none', background: isActive ? '#4E79A7' : 'transparent', color: isActive ? '#fff' : '#64748b', fontSize: 12, fontWeight: 600, fontFamily: 'Georgia, serif', cursor: 'pointer', whiteSpace: 'nowrap', transition: 'all 0.15s ease', }} > {label} </button> ) })} </div> )} <div style={{ flex: 1, minHeight: 0, paddingTop: 'var(--app-nav-height)', touchAction: 'none', position: 'relative', }} > <EuclidCanvas key={`${propId}-${proofVariant ?? 'canonical'}`} propositionId={propId} proposition={selectedProp} onComplete={handleComplete} languageStyle={languageStyle} completionMeta={{ unlocked, nextPropId, onNavigateNext: (id) => navigateWithPlayer(`/toys/euclid/${id}`), onNavigateMap: () => navigateWithPlayer('/toys/euclid'), }} /> </div> </div> ) } |